#include "namedPipe.hpp"

int main()
{
    // 创建命名管道
    bool ret = createFifo(PATH_NAME, MY_MODE);
    assert(ret);
    (void)ret;

    // 打开管道文件
    std::cout << "server is running ..." << std::endl;
    int readFd = open(PATH_NAME, O_RDONLY);
    std::cout << "server run finished" << std::endl;
    if(readFd < 0) exit(1);// fd == -1

    // 读取数据
    char buffer[1024];
    while(true)
    {
        ssize_t n = read(readFd, &buffer, sizeof(buffer) - 1);
        if(n > 0)
        {
            buffer[n] = 0;
            std::cout << "read success -> client say:" << buffer << std::endl;
        }
        else if(n == 0)
        {
            // 读取到文件结束
            std::cout << "read finished! client have quitted and shall I!" << std::endl;
            break;
        }
        else
        {
            std::cout << "errno:" << errno << " -> " << strerror(errno) << std::endl;
            break;
        }
    }
    close(readFd);

    // 删除管道
    deleteFifo(PATH_NAME);

    return 0;
}